package com.esunny.resourcevisualization.controller;

import com.esunny.resourcevisualization.domain.request.ResourceTypeEntityDto;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.service.ExcelOperationService;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @version 1.0.0.0
 * @ClassName ExcelOperationController
 * @Description excel文件处理类
 * @Author baochencong
 * @Date 2020/11/2 13:51
 */
@Controller
@RequestMapping("/excel")
public class ExcelOperationController {

    @Resource
    private ExcelOperationService excelOperationService;

    /**
     * 导入资源实体excel文件接口
     *
     * @param file excel文件
     * @return 数据导入结果
     */
    @ResponseBody
    @PostMapping("/importResourceData")
    public ReturnStructure importResourceData(@NotNull MultipartFile file) {
        if (file.isEmpty()) {
            return ReturnStructure.ERROR("上传失败", null);
        }

        return excelOperationService.importResourceData(file);
    }

    /**
     * 下载导入数据模板
     *
     * @param name 资源类别英文名
     * @return 下载结果；
     */
    @ResponseBody
    @GetMapping("/downloadTemplate/{name}")
    public ReturnStructure downloadTemplate(@PathVariable("name") String name) {
        if (StringUtils.isBlank(name)) {
            return ReturnStructure.NOT_FOUND();
        }

        return excelOperationService.downloadTemplate(name);
    }

    /**
     * 下载某资源类别下所有实体资源
     *
     * @param name 资源类别英文名
     * @return 下载结果
     */
    @ResponseBody
    @GetMapping("/downloadResourceData/{name}")
    public ReturnStructure downloadResourceData(@PathVariable("name") String name) {
        if (StringUtils.isBlank(name)) {
            return ReturnStructure.NOT_FOUND();
        }

        return excelOperationService.downloadResourceData(name);
    }

    /**
     * 下载选中的实体资源
     *
     * @param entityListDto 选中的实体资源列表
     * @return 下载结果
     */
    @ResponseBody
    @PostMapping("/downloadSelectedResourceData")
    public ReturnStructure downloadSelectedResourceData(@RequestBody ResourceTypeEntityDto entityListDto) {
        if (entityListDto.getResourceTypeEntityList().size() <= 0) {
            return ReturnStructure.NOT_FOUND();
        }

        return excelOperationService.downloadSelectedResourceData(entityListDto.getResourceTypeEntityList());
    }

}
